const SERVER = require("./interface")
module.exports = {
  refreshOrders,
  getAllOrders,
  getOrderNum,
  clear,
  haveCurrentOrder,
  refreshCurrentOrder,
  getCurrentOrder
}

let orders = []
let currentOrder = undefined

async function refreshOrders() {
  let result = await SERVER.queryHistoryOrderList()
  if (result.status == "success") {
    orders = result.res
    return true
  } else {
    return false
  }
}

function getAllOrders() {
  return orders
}

function getOrderNum() {
  return orders.length
}

function clear() {
  orders = []
}

function haveCurrentOrder() {
  return currentOrder !== undefined
}

async function refreshCurrentOrder() {
  let result = await SERVER.queryCurrentOrder()
  if (result.status != "success")
    return false
  currentOrder = result.res
  return true
}

function getCurrentOrder() {
  return currentOrder
}